package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.DeliveryAddressDao;
import com.foreknow.elm.dao.impl.DeliveryAddressDaoImpl;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.DeliveryAddress;
import com.foreknow.elm.service.DeliveryAddressService;
import com.foreknow.elm.util.DBUtil;

import java.util.ArrayList;
import java.util.List;

public class DeliveryAddressServiceImpl implements DeliveryAddressService {
    @Override
    public List<DeliveryAddress> listDeliveryAddressByUserId(String userId) {
        List<DeliveryAddress> list = new ArrayList<>();
        DeliveryAddressDao deliveryAddressDao = new DeliveryAddressDaoImpl();
      try{
          DBUtil.getConnection();
          list = deliveryAddressDao.listDeliveryAddressByUserId(userId);
      }catch (Exception e) {
          e.printStackTrace();
      }finally {
          DBUtil.close();
      }
        return list;
    }

    @Override
    public DeliveryAddress getDeliveryAddressById(int daId) {
        DeliveryAddressDaoImpl deliveryAddressDao = new DeliveryAddressDaoImpl();
        DeliveryAddress deliveryAddress = new DeliveryAddress();
        try {
            DBUtil.getConnection();
            deliveryAddress = deliveryAddressDao.getDeliveryAddressById(daId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return deliveryAddress;
    }

    @Override
    public int saveDeliveryAddress(String contactName, int contactSex, String contactTel, String address, String userId) {
        DeliveryAddressDao deliveryAddressDao = new DeliveryAddressDaoImpl();
        int isRight = 0;
        try {
            DBUtil.getConnection();
            isRight = deliveryAddressDao.saveDeliveryAddress(contactName, contactSex, contactTel, address, userId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return isRight;
    }

    @Override
    public int updateDeliveryAddress(int daId, String contactName, int contactSex, String contactTel, String address, String userId) {
        DeliveryAddressDao deliveryAddressDao = new DeliveryAddressDaoImpl();
        int isRight = 0;
        try {
            DBUtil.getConnection();
            isRight = deliveryAddressDao.updateDeliveryAddress(daId,contactName,contactSex,contactTel,address,userId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return isRight;
    }

    @Override
    public int removeDeliveryAddress(int daId){
        int isRight = 0;
        DeliveryAddressDao deliveryAddressDao = new DeliveryAddressDaoImpl();
        try {
            DBUtil.getConnection();
            isRight = deliveryAddressDao.removeDeliveryAddress(daId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return isRight;
    }
}
